package gov.pbc.nn.kjgl.gzjh.view;

import java.util.List;
import java.util.Map;
import gov.pbc.nn.core.entity.BaseEntity;
import gov.pbc.nn.kjgl.gzjh.dao.WorkDao;
import gov.pbc.nn.kjgl.gzjh.dao.WorkJdbcDao;
import gov.pbc.nn.kjgl.gzjh.entity.WorkContent;
import gov.pbc.nn.kjgl.gzjh.entity.WorkP;
import gov.pbc.nn.kjgl.gzjh.entity.WorkPlanBanch;
import gov.pbc.nn.kjgl.gzjh.entity.WorkPlanProcessUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

/**
 * 
 * @author jack.zhao
 * 
 */
@Component("workPlanPR")
public class WorkPlanPR {
	@Autowired
	private WorkDao workkDao;

	@Autowired
	private WorkJdbcDao jdbcDao;

	/**
	 * 处理人页面返回分页方法
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * @param type
	 * @throws Exception
	 * 
	 */
	@DataProvider
	public void loadWorks(Page<WorkP> page, Criteria criteria, String type) throws Exception {
		this.workkDao.loadWorks(page, criteria);
	}

	/***
	 * 科长页面返回分页方法
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * @throws Exception
	 * 
	 */
	@DataProvider
	public void loadkzWorks(Page<WorkP> page, Criteria criteria) throws Exception {
		this.jdbcDao.getAllkz(page, criteria);
	}

	/**
	 * 处理人页面加载后台实体方法
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * @throws Exception
	 * 
	 */
	@DataProvider
	public void loadUserWorks(Page<WorkP> page, Criteria criteria) throws Exception {
		this.jdbcDao.getAllUser(page, criteria);
	}

	/**
	 * 公用页面的根据workId查找workBanch
	 * 
	 * @auther jack.zhao
	 * @param workId
	 * @return
	 * 
	 */
	@DataProvider
	public List<WorkPlanBanch> getWorkPlanBanchById(String workId) {
		return workkDao.getWorkPlanBanchById(workId);
	}

	/**
	 * 根据workId查找匹配的科长
	 * 
	 * @auther jack.zhao
	 * @param workId
	 * @param banchUsername
	 * @return
	 * 
	 */
	@DataProvider
	public List<WorkPlanBanch> getWorkPlanBanchByDept(String workId, String banchUsername) {
		// return workkDao.getWorkPlanBanchByDept(workId);
		return this.jdbcDao.getWorkBanchByDeptProcess(workId, banchUsername);
	}

	// 根据科长名字找到workPlanBanch
	@DataProvider
	public List<WorkPlanBanch> UsernamegetWorkPlanBanchBy(String workId) {
		return workkDao.getWorkPlanBanchByUsername(workId);
	}

	@DataProvider
	public List<WorkContent> getWorkContentById(String bancherId) {
		// if (workkDao.getWorkContentById(bancherId).size() > 0) {
		return workkDao.getWorkContentById(bancherId);
	}

	/**
	 * 公用页面的根据bancherId查找workPlanProcessuser
	 * 
	 * @auther jack.zhao
	 * @param bancherId
	 * @return
	 * 
	 */
	@DataProvider
	public List<WorkPlanProcessUser> getWorkPlanProcessUserById(String bancherId) {
		return workkDao.getWorkPlanProcessUserById(bancherId);
	}

	/**
	 * 任务处理人界面要查自己的任务
	 * 
	 * @auther jack.zhao
	 * @param bancherId
	 * @return
	 * 
	 */
	@DataProvider
	public List<WorkPlanProcessUser> getWorkPlanProcessUserByIdByUsername(String bancherId) {
		return workkDao.getWorkPlanProcessUserByIdByUsername(bancherId);
	}

	@Transactional
	@DataResolver
	public void saveAndUpdate(WorkP WorkEntity) throws Exception {
		// TaskEntity entity = EntityUtils.toEntity(WorkEntity);
		this.workkDao.saveAndUpdateEntity(WorkEntity);
		List<WorkPlanBanch> workPlanBanchs = EntityUtils.getValue(WorkEntity, "banchUser");
		this.saveBancher(workPlanBanchs, WorkEntity.getId());
	}

	@Transactional
	@DataResolver
	public void saveAllandUpdate(List<WorkP> workps) throws Exception {
		for (WorkP p : workps) {
			this.saveAndUpdate(p);
		}
	}

	public void saveBancher(List<WorkPlanBanch> workPlanBanchs, String wId) throws Exception {
		if (workPlanBanchs != null) {
			for (WorkPlanBanch wb : workPlanBanchs) {
				wb.setWorkId(wId);
				this.workkDao.saveAndUpdateEntity((BaseEntity) wb);
				List<WorkContent> WorkContents = EntityUtils.getValue(wb, "workContent");
				this.saveWorkContent(WorkContents, wb.getId(), wId);
				//
				List<WorkPlanProcessUser> workPlanProcessUsers = EntityUtils.getValue(wb, "processUser");
				this.saveProcessUser(workPlanProcessUsers, wb.getId(), wId);
			}
		}

	}

	public void saveWorkContent(List<WorkContent> WorkContents, String banchId, String wId) throws Exception {
		if (WorkContents != null) {
			for (WorkContent w : WorkContents) {
				w.setWorkId(wId);
				w.setUserId(banchId);
				this.workkDao.saveAndUpdateEntity((BaseEntity) w);
				
				
			}
		}
	}

	public void saveProcessUser(List<WorkPlanProcessUser> workPlanProcessUsers, String banchId, String wId) throws Exception {
		if (workPlanProcessUsers != null) {
			for (WorkPlanProcessUser pu : workPlanProcessUsers) {
				pu.setBanchId(banchId);
				pu.setWorkId(wId);
				
				
				//更新处理人的实体
				List<WorkContent> WorkContents = EntityUtils.getValue(pu, "workContent");
				this.saveWorkContent(WorkContents, pu.getId(), wId);
				
				this.workkDao.saveAndUpdateEntity((BaseEntity) pu);
			}
		}
	}

	@Expose
	@Transactional
	public void deleteBanch(String banchId) {
		this.workkDao.delete(banchId, WorkPlanBanch.class);
	}

	@Expose
	@Transactional
	public void deleteContent(String contentId) {
		this.workkDao.delete(contentId, WorkContent.class);
	}

	@Expose
	@Transactional
	public void deleteProcessUser(String userId) {
		//根据id删除
		this.workkDao.delete(userId, WorkPlanProcessUser.class);
		//迭代workCon
		List<WorkContent> workContents = this.workkDao.getWorkContentById(userId);
		for (WorkContent w : workContents) {
			System.out.println(w);
			this.workkDao.delete(w);
		}
	}

	@Expose
	@Transactional
	public void deleteWork(String workId) {
		this.workkDao.delete(workId, WorkP.class);
		List<WorkPlanBanch> workPlanBanchs = this.workkDao.getWorkPlanBanchById(workId);
		for (WorkPlanBanch w : workPlanBanchs) {
			List<WorkPlanProcessUser> planProcessUsers = this.workkDao.getWorkPlanProcessUserById(w.getId());
			for (WorkPlanProcessUser pu : planProcessUsers) {
				List<WorkContent> workContents = this.workkDao.getWorkContentById(pu.getId());
				for (WorkContent wc : workContents) {
					this.workkDao.delete(wc);
				}
				this.workkDao.delete(pu);
			}
			List<WorkContent> wcs = this.workkDao.getWorkContentById(w.getId());
			for (WorkContent wc : wcs) {
				this.workkDao.delete(wc);
			}
			this.workkDao.delete(w);
		}

	}

	@DataResolver
	@Transactional
	public void saveAndUpdateBancer(List<WorkPlanBanch> wbs) throws Exception {
		for (WorkPlanBanch wb : wbs) {
			this.workkDao.saveAndUpdateEntity((BaseEntity) wb);
		}
	}

	@Transactional
	@DataResolver
	public void saveAndUpdateContent(List<WorkContent> wc) throws Exception {
		for (WorkContent w : wc) {
			this.workkDao.saveAndUpdateEntity((BaseEntity) w);
		}

	}

	// ---------------------------------------------------科长的页面
	@DataResolver
	@Transactional
	public void saveAndUpdateUser(List<WorkPlanProcessUser> wpus) throws Exception {
		for (WorkPlanProcessUser pu : wpus) {
			this.workkDao.saveAndUpdateEntity((BaseEntity) pu);
		}
	}

	@Expose
	@Transactional
	public void tj(String id) {
		WorkP workP = this.workkDao.get(id, WorkP.class);
		workP.setState(2);
		this.workkDao.update(workP);
	}

}
